<?php

defined('BASEPATH') OR exit('No direct script access allowed');

// This can be removed if you use __autoload() in config.php OR use Modular Extensions
require APPPATH . '/libraries/REST_Controller.php';

class Example extends REST_Controller {

    function car_get() {
        if (!$this->get('id')) {
            $this->response(NULL, 400);
        }
        $car = $this->car_model->get($this->get('id'));
        if ($car) {
            $this->response($car, 200); // 200 being the HTTP response code  
        } else {
            $this->response(NULL, 404);
        }
    }

    function car_post() {
        $result = $this->car_model->update($this->post('id'), array(
            'name' => $this->post('name'),
            'email' => $this->post('email')
                ));
        if ($result === FALSE) {
            $this->response(array('status' => 'failed'));
        } else {
            $this->response(array('status' => 'success'));
        }
    }

    function car_put() {
        $data = array('returned: ' . $this->put('id'));
        $this->response($data);
    }

    function car_delete() {
        $data = array('returned: ' . $this->delete('id'));
        $this->response($data);
    }

    function cars_get() {
        $cars = $this->car_model->get_all();
        if ($cars) {
            $this->response($cars, 200);
        } else {
            $this->response(NULL, 404);
        }
    }

}